#include <ctime>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
void swap(std::vector<int>&arr,int i,int j){
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j]=temp;
}

void heap_up(std::vector<int> &arr,int heap_size){
    int child = heap_size -1;
    int father = (heap_size-1)/2 ;
    if(father >= 0 && arr[child]<arr[father]){
        swap(arr,child,father);
        heap_up(arr,father+1);
    }

}

void heap_build(std::vector<int> &arr,int heap_size){
    for(int i =1;i<heap_size;i++){
        heap_up(arr,i);
    }
}

void print(std::vector<int> &arr) {
    for( size_t i =0;i<arr.size();i++){
        cout<<arr[i]<<" ";
        if((i+1)%10 == 0){
            cout<<endl;
        }
    }
}

void test(){

}

int main()
{
    std::srand(std::time(0));
    std::vector<int>arr;
    for(int i=0;i<20;i++){
        arr.push_back(std::rand()%20);
    }
    print(arr);
    heap_build(arr,10);
    print(arr);
    cout<<"-----------------"<<endl;
    for(int i=10;i<20;i++){
        if(arr[0]<arr[i]){
            swap(arr,0,i);
            heap_build(arr,10);
        }
    }
    print(arr);

    test ();
    return 0;
}

